<template>
	<view class="dynamic_route">
		<xbd-pages-content title="规划动态线路">
			<template #content>
				<view class="content" :style="'minHeight:'+windowHeight+'px'">
					<view class="type" v-for="(item,index) in site_list" :key = 'index'>
						<text>经过点 {{index+1}}</text>
						<text class="input text">{{item}}</text>
					</view>
					<view class="type">
						<text>停靠时间</text>
						<xbd-select :carId.sync="formData.via_time" :visible.sync="visible" :infoList="typeArr">
							<template>
								<view class="select" @click="visible = true">
									<text> {{formData.via_time || '请选择停靠时间'}} </text>
									<image class="image" src="@/static/images/health/right.svg" mode=""></image>
								</view>
							</template>
						</xbd-select>
					</view>
					<view class="type">
						<text>返回点</text>
						<xbd-select :carId.sync="type" :visible.sync="visible2" :infoList="typeArr1" @selectSite ="selectSite">
							<template>
								<view class="select" @click="visible2 = true">
									<text> {{type || '请选择站点'}} </text>
									<image class="image" src="@/static/images/health/right.svg" mode=""></image>
								</view>
							</template>
						</xbd-select>
					</view>
					<view class="type">
						<text>停靠时间</text>
						<xbd-select :carId.sync="formData.end_time" :visible.sync="visible3" :infoList="typeArr">
							<template>
								<view class="select" @click="visible3 = true">
									<text> {{formData.end_time || '请选择停靠时间'}} </text>
									<image class="image" src="@/static/images/health/right.svg" mode=""></image>
								</view>
							</template>
						</xbd-select>
					</view>
					<view class="choose_num">
						<text>执行车辆（请选择执行车辆）</text>
						<view class="box">
							<view :class="['num', num===index ? 'active' : '']"  v-for="(item,index) in robot_list" :key="index" @click="numBtn(index,item.robot_id)">
								{{item.robot_object_name}}
							</view>
							<!-- <view :class="['num', num2==='2' ? 'active' : '']" @click="numBtn2()">
								182
							</view> -->
						</view>
					</view>
					<xbd-popup :isShow="showDialog" @dialogConfirm="dialogConfirm" @dialogCancel="dialogCancel"
						content="确定要规划路线吗？" cancelText="取消" confirmText="确定">
					</xbd-popup>
					<view class="bottom">
						<view class="button" @click="add()">规划并把所选订单加入线路</view>
					</view>
				</view>

			</template>
		</xbd-pages-content>
	</view>
</template>

<script>
	import { mapState } from 'vuex';
	import { DeliveryOrder } from '@/api/index.js';
	export default {
		data() {
			return {
				num :1000,
				flag:true,
				visible: false,
				visible2: false,
				visible3: false,
				showDialog:false,
				order_id_list:[],
				robot_list:[],
				type:'',
				typeArr: [
					{
						value: '0',
						label: '5min',
						state:''
					},
					{
						value: '1',
						label: '10min',
						state:''
					},
					{
						value: '2',
						label: '15min',
						state:''
					},
					{
						value: '3',
						label: '20min',
						state:''
					},
					{
						value: '4',
						label: '25min',
						state:''
					},
					{
						value: '5',
						label: '30min',
						state:''
					},
					
				],
				typeArr1: [
				],
				windowHeight: 0,
				site_list:{
					
				},
				formData: {
					via1:'京东超市',
					via2:'行政楼',
					end_time:'',
					via_time:'',
					site: '',
					car:''
				},
				info:{}
			};
		},
		methods: {
			dialogConfirm() {
				if(this.flag){
					this.flag = false
					this.submit()
				}
				
				this.showDialog = false;
			},
			
			selectSite(value){
				this.formData.site = value
			},
			dialogCancel() {
				this.showDialog = false;
			},
			numBtn(index,val){
				this.num = index
				this.formData.car = val
			},
			add() {
				this.info = {
					order_id_list:this.order_id_list,
					after_docking_time: parseInt(this.formData.via_time),
					back_id:this.formData.site,
					back_docking_time:parseInt(this.formData.end_time),
					car_id:this.formData.car
					
				}
				if (!this.formData.via_time.length) {
					this.$showToast('请选择停靠时间');
				} else if (!this.formData.site) {
					this.$showToast('请选择返回点');
				} else if (!this.formData.end_time.length) {
					this.$showToast('请选择停靠时间');
				}else if (!this.formData.site.length) {
					this.$showToast('请选择执行车辆');
				}else{
					this.showDialog = true
				}
			},
			async submit(){
				try {
					this.$showLoading('加载中');
					const res = await DeliveryOrder.postCreateDynamic(this.info);
					uni.hideLoading();
					if(res.success){
						uni.$emit('successDynamic',this.order_id_list);
						setTimeout(() => {
							uni.hideLoading();
							uni.navigateBack({
								delta: 1
							});
						}, 1000)
					}else{
						this.flag = true
						this.$showToast(res.msg);
					}
				} catch (err) {
					uni.showToast({
						title: err[0].errorMessage,
						icon: 'none'
					})
				}
			},
			async getSite(){
				try {
					const res = await DeliveryOrder.getSiteList();
					this.typeArr1 = res.data.list
				} catch (err) {
					uni.showToast({
						title: err[0].errorMessage,
						icon: 'none'
					})
				}
			},
			async init(id){
				const o ={
					order_id_list : id
				}
				try {
					this.$showLoading('加载中');
					const res = await DeliveryOrder.postCarList(o);
					uni.hideLoading();
					this.site_list = res.data.site_list
					this.robot_list = res.data.robot_list
					
				} catch (err) {
					uni.hideLoading();
				}
			},
		},
		computed: {},
		onLoad() {
			uni.getStorage({
			    key: 'routeChoose',
			    success: (res) => {
					this.order_id_list = res.data
					this.init(res.data)
			    }
			});
			this.getSite()
			uni.getSystemInfo({
				success: (res) => {
					this.windowHeight = res.windowHeight
				}
			});
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep .uni-easyinput__content-input {
		text-align: right;
	}

	::v-deep .is-input-border {
		border: none;
	}

	::v-deep .is-disabled {
		background-color: #FFFFFF !important;
	}

	.content {
		padding: 40rpx;
		background-color: #FFFFFF;
		box-sizing: border-box;
		.choose_num{
			box-sizing: border-box;
			padding: 20rpx 0;
			border-bottom: 2rpx solid #E8E2E2;
			font-size: 28rpx;
			.box{
				display: flex;
				margin-top: 20rpx;
				.num{
					margin-right: 30rpx;
					padding: 0 10rpx;
					height: 44rpx;
					text-align: center;
					line-height: 40rpx;
					border-radius: 8rpx;
					box-sizing: border-box;
					border: 2rpx solid #457AFF;
					color: #457AFF;
				}
				.active{
					background-color: #457AFF !important;
					color: #FFFFFF;
				}
			}
		}
		.type {
			box-sizing: border-box;
			padding: 20rpx 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 2rpx solid #E8E2E2;
			font-size: 28rpx;

			.text {
				margin: 20rpx;
			}

			.input {
				text-align: right;
				font-size: 28rpx !important;
				color: #394954 !important;
			}

			.select {
				display: flex;
				justify-content: space-between;
				align-items: center;
				height: 28px;
				line-height: 28px;
				box-sizing: border-box;
				padding: 0 20rpx 0 20rpx;
				color: #999999 !important;

				.image {
					transform: rotate(90deg);
					width: 38rpx;
					height: 38rpx;
				}
			}
		}

		.bottom {
			display: flex;
			justify-content: center;
			margin-top: 80rpx;

			.button {
				width: 480rpx;
				height: 70rpx;
				line-height: 66rpx;
				text-align: center;
				border-radius: 8rpx;
				font-size: 28rpx;
				color: #FFFFFF;
				background-color: #457AFF;
			}
		}
	}
</style>
